package com.shenzhen.horselive.widget;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MapReclyerView extends RecyclerView {
    public MapReclyerView(@NonNull Context context) {
        super(context);
    }

    public MapReclyerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MapReclyerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        int x = (int) e.getX();
        int y = (int) e.getY();
        int point = pointToPosition(x, y);

        //如果点 的是地图,这里我的地图是在第二个item里面所以是1
        if (point ==0) {
            ViewGroup view = (ViewGroup) getChildAt(point);
            if (view != null) {
                return false;
            }
        }
        return super.onInterceptTouchEvent(e);
    }

    private static final int INVALID_POSITION = -1; // 触摸到的点不在子View范围内
    private Rect mTouchFrame;   // 子View所在的矩形范围

    public int pointToPosition(int x, int y) {
        int firstPosition = ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition();
        Rect frame = mTouchFrame;
        if (frame == null) {
            mTouchFrame = new Rect();
            frame = mTouchFrame;
        }

        final int count = getChildCount();
        for (int i = count - 1; i >= 0; i--) {
            final View child = getChildAt(i);
            if (child.getVisibility() == View.VISIBLE) {
                child.getHitRect(frame);
                if (frame.contains(x, y)) {
                    return firstPosition + i;
                }
            }
        }
        return INVALID_POSITION;
    }


}
